// pages/booking/booking.js
const request=require('../../util/request.js')
const navBarHeight = wx.getSystemInfoSync().statusBarHeight + 44;
Page({
  data: {
    book_id:0,
    navBarHeight:navBarHeight,
    tabIndex:0,
    amount:"0.00",
    mindate:Math.round((new Date() - 365*86400*1000*5)),
    tags:[],
    date:"",
    from_account:{
      title:"转出账户",
      desc:"",
      color:"",
      icon:"",
      id:0
    },
    to_account:{
      title:"转入账户",
      desc:"",
      color:"",
      icon:"",
      id:0
    },
    exchange_rate:0,
    desc:"",
    profit_points:0,
    account:null,
    showDate:false,
    sr_default:{},
    zc_default:{},
    customer:null,
    invoice:null,
    from_currency:""
  },
  closePage(){
    wx.navigateBack()
  },
  onSelectDate(){
    this.setData({
      showDate:true
    })
  },
  save(){


    var that = this;
    var data = that.data;
    request.requestAjax('account/booking',{data:data},"加载中",function(res){
        if(res.status==200){
          wx.showToast({
            title: '保存成功',
            icon:"success"
          })
          setTimeout(function(){
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];
        
            if(Reflect.has(prevPage,"getLogs")){
              prevPage.getLogs();
            }

            if(Reflect.has(prevPage,"getAccountInfo")){
              prevPage.getAccountInfo();
            }
              wx.navigateBack();
          },1000);
            return false;
        }else{
          wx.showToast({
            title: res.msg,
            icon:"none"
          })
        }

    },function(res){
      wx.showToast({
        title: '提交失败',
        icon:"none"
      })
    });
  },
  onSubmit(e){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '您确定保存吗？',
      success: function (res) {
        if (res.confirm) { //这里是点击了确定以后
          that.save();
        } 
      }
    })

  },
  onDateClose(){
    this.setData({
      showDate:false
    })
  },
  onDateConfirm(e){
    console.log(e)
    this.setData({
      date:this.formatDate(e.detail),
      showDate:false
    })
  },
  formatDate(date) {
    date = new Date(date);
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    month = month.toString();
    if(month.length==1){
      month = "0"+month;
    }

    var day = date.getDate();
    day = day.toString();

    if(day.length==1){
      day = "0"+day;
    }
    return `${year}/${month}/${day}`;
  },
  inputAmount(e){
    // console.log(this.formatMoney(e.detail.value.current))
    this.setData({
      amount:this.formatMoney(e.detail.value.current)
    })
  },
  goPage(event){
    // console.log(event);
    if(event.currentTarget.dataset.pageurl=="/pages/register/register"){
      Toast('暂未开放注册~');
    }

    wx.navigateTo({
      url: event.currentTarget.dataset.pageurl,
    })
  },
  formatMoney(value){
    value = value.toString();
    var temp;
    temp = value.split('.')
    var xs = "";
    if(temp[0]==undefined || temp[0]==""){
      temp[0] = "0";
    }



    if(temp[1]==undefined){
      xs = "00";
    }else{
      if(temp[1].length==0){
        xs = "00";
      }
      if(temp[1].length==1){
        xs = temp[1]+"0"
      }

      if(temp[1].length==2){
        xs = temp[1]
      }

    }

    return temp[0]+"."+xs;
  },
  setTabIndex(e){
    console.log(e)
    this.setData({
      tabIndex:e.currentTarget.dataset.index
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getBookingTag()
    if(options.book_id!=undefined){
      console.log(options.book_id)
      this.setData({
        book_id:options.book_id
      })
    }

    if(options.account_id!=undefined){
      this.getAccountInfo(options.account_id);
    }
  },
  getAccountInfo(account_id){
    var that = this;
    request.requestAjax('company/accountInfo',{id:account_id},"加载中",function(res){
        if(res.status==200){
            that.setData({
              account:{
                    id:res.data.id,
                    title:res.data.title
              }
            });
            return false;
        }else{
          wx.showToast({
            title: '请求失败',
            icon:"none"
          })
        }
  
    },function(res){
      wx.showToast({
        title: '请求失败',
        icon:"none"
      })
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },
  selectZcTag(e){
    var index = e.currentTarget.dataset.index;
    this.setData({
      zc_default:this.data.tags[1][index]
    })
  },
  selectSrTag(e){
    var index = e.currentTarget.dataset.index;
    this.setData({
      sr_default:this.data.tags[0][index]
    })
  },
  getBookingTag(){
    var that = this;
    request.requestAjax('company/getBookingTag',{},"加载中",function(res){
        if(res.status==200){
            that.setData({
              tags:res.data.data,
              sr_default:res.data.sr_default,
              zc_default:res.data.zc_default
            })
            console.log(that.data.tags)
            return false;
        }else{
          wx.showToast({
            title: '请求失败',
            icon:"none"
          })
        }
  
    },function(res){
      wx.showToast({
        title: '请求失败',
        icon:"none"
      })
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})